package com.marry.daodao.authserver.applcation.service.impl;

import com.marry.daodao.authserver.applcation.service.UserService;
import com.marry.daodao.authserver.domain.User;
import com.marry.daodao.authserver.infrastructure.repository.UserRepository;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.BadClientCredentialsException;
import org.springframework.stereotype.Service;

/**
 * @author mal
 * @date 2022-11-15 10:59
 */
@Service

public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;

    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或者密码错误");
        }

        if (user.isCanceled()) {
            throw new BadCredentialsException("用户已经被取消");
        }

        return user;
    }
}
